package com.example.demo1105.demo3;

import java.util.*;
import java.util.stream.Collectors;
/**
 * @Author: FBS
 * @Description: TODO
 * @DateTime: 2022/11/6 20:23
 *@Version:1.0
 */
public class CollectThree {
    public static void main(String[] args) {
        ArrayList<Student> student = new ArrayList<>();
        student.add(new Student("薛之谦",18,"男"));
        student.add(new Student("罗永浩", 25, "男"));
        student.add(new Student("野猪佩奇", 21, "女"));
        student.add(new Student("杨超越", 22, "女"));
        student.add(new Student("马化腾", 15, "男"));
        student.add(new Student("马云", 19, "男"));
        student.add(new Student("露娜", 23, "女"));
        Long collect = student.stream().collect(Collectors.counting());
        System.out.println("collect = " + collect);

        Double collect1 = student.stream().collect(Collectors.averagingInt(item -> item.getAge()));
        System.out.println("collect1 = " + collect1);

        Optional<Student> collect2 = student.stream().collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println("collect2 = " + collect2);

        Optional<Student> collect3 = student.stream().collect(Collectors.minBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println("collect3 = " + collect3);

        Integer collect4 = student.stream().collect(Collectors.summingInt(item -> item.getAge()));
        System.out.println("collect4 = " + collect4);

        IntSummaryStatistics collect5 = student.stream().collect(Collectors.summarizingInt(item -> item.getAge()));
        System.out.println("collect5 = " + collect5);

        List<String> list =student.stream()
                .filter(item -> item.getAge()> 18)
                .filter(item -> item.getSex().equals("男"))
                .map(item -> item.getName())
                .collect(Collectors.toList());
        System.out.println("list = " + list);

        Set<String> set =student.stream()
                .filter(item -> item.getAge()>18)
                .filter(item -> item.getSex().equals("男"))
                .map(item -> item.getName())
                .collect(Collectors.toSet());
        System.out.println("set = " + set);

        Map<String,Student> map =student.stream()
                .filter(item -> item.getAge()>18)
                .filter(item -> item.getSex().equals("男"))
                .collect(Collectors.toMap(item -> item.getName(),item -> item));
        System.out.println("map = " + map);

        Map<String, List<Student>> collect6 = student.stream().collect(Collectors.groupingBy(item -> item.getSex()));
        System.out.println("collect6 = " + collect6);


    }
}
class Student{
    private String name;
    private Integer age;
    private String sex;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    public Student(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
